package org.ben.birthday.twentybirthday.service.impl;

import org.ben.birthday.twentybirthday.dao.UserDaoRepository;
import org.ben.birthday.twentybirthday.entity.User;
import org.ben.birthday.twentybirthday.service.IUserService;
import org.ben.birthday.twentybirthday.utils.Result;
import org.ben.birthday.twentybirthday.utils.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2019/7/18 18:02
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    UserDaoRepository userDaoRepository;

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    /**
     * 用户注册ServiceImpl
     * @param user
     * @param httpSession
     * @return Msg
     */
    @Override
    public boolean registService(User user, HttpSession httpSession) {
        List<User> userList=userDaoRepository.findUsersByUserName(user.getUserName());
        Iterator<User> userIterator=userList.iterator();
        while (userIterator.hasNext()){
                logger.error("用户名已存在，注册失败");
               return false;
        }
        user.setLevel(1);
        userDaoRepository.save(user);
        logger.info("注册成功");
        httpSession.setAttribute("userName",user.getUserName());
        httpSession.setAttribute("level",user.getLevel());
        httpSession.setAttribute("identity","用户");
        return true;
    }

    /**
     * 用户名密码登录ServiceImpl
     * @param userName
     * @param password
     * @param httpSession
     * @return Result
     */
    @Override
    public Result loginUserNameService(String userName, String password,HttpSession httpSession) {
        List<User> userList=userDaoRepository.findUsersByUserName(userName);
        Iterator<User> userIterator=userList.iterator();
        while(userIterator.hasNext()){
            User user=userIterator.next();
            if (user.getUserName().equals(userName)&&user.getPassword().equals(password)&&user.getLevel()>=1){
                logger.info("账号密码正确,用户登录成功");
                httpSession.setAttribute("userName",userName);
                httpSession.setAttribute("level",user.getLevel());
                httpSession.setAttribute("identity","用户");
            return new Result(ResultCode.User,"普通用户登录成功");
            }else if(user.getUserName().equals(userName)&&user.getPassword().equals(password)&&user.getLevel()==0){
                logger.info("账号密码正确，管理员登录成功");
                httpSession.setAttribute("userName",userName);
                httpSession.setAttribute("level",user.getLevel());
                httpSession.setAttribute("identity","管理员");
                return new Result(ResultCode.Administrator,"管理员登录成功");
            }else {
                logger.error("账号密码错误，登录失败");
                return new Result(ResultCode.Fail,"用户名或者密码不符，登录失败");
            }
        }
        return new Result(ResultCode.Fail,"该用户不存在");
    }

    /**
     * 邮箱验证码登录ServiceImpl
     * @param httpSession
     * @param userName
     * @param email
     * @param verificationCode
     * @return Result
     */
    @Override
    public Result loginEmailService(HttpSession httpSession, String userName,String email,String verificationCode) {
        List<User> userList=userDaoRepository.findUsersByUserName(userName);
        Iterator<User> userIterator=userList.iterator();
        while(userIterator.hasNext()){
            User user=userIterator.next();
            if (user.getUserName().equals(userName)&&user.getEmail().equals(email)&&httpSession.getAttribute("loginverificationCode").equals(verificationCode)&&user.getLevel()>=1){
                logger.info("账号密码正确,用户登录成功");
                httpSession.setAttribute("userName",userName);
                httpSession.setAttribute("level",user.getLevel());
                httpSession.setAttribute("identity","用户");
                return new Result(ResultCode.User,"普通用户登录成功");
            }else if(user.getUserName().equals(userName)&&user.getEmail().equals(email)&&httpSession.getAttribute("loginverificationCode").equals(verificationCode)&&user.getLevel()==0){
                logger.info("账号密码正确，管理员登录成功");
                httpSession.setAttribute("userName",userName);
                httpSession.setAttribute("level",user.getLevel());
                httpSession.setAttribute("identity","管理员");
                return new Result(ResultCode.Administrator,"管理员登录成功");
            }else {
                logger.error("验证码错误，登录失败");
                return new Result(ResultCode.Fail,"验证码错误，登录失败");
            }
        }
        return new Result(ResultCode.Fail,"该用户不存在");
    }

    /**
     * 根据用户名和原密码更改密码ServiceImpl
     * @param userName
     * @param usedPassword
     * @param updatePassword
     * @param email
     * @param httpSession
     * @param verificationCode
     * @return boolean
     */
    @Override
    public boolean updatePasswordService(String userName, String usedPassword,String updatePassword,String email,HttpSession httpSession,String verificationCode) {
    List<User> userList=userDaoRepository.findUsersByUserName(userName);
    Iterator<User> userIterator=userList.iterator();
    while(userIterator.hasNext()){
        User user=userIterator.next();
        if (user.getPassword().equals(usedPassword)&&user.getEmail().equals(email)&&httpSession.getAttribute("updatePasswordverificationCode").equals(verificationCode)){
            user.setPassword(updatePassword);
            userDaoRepository.saveAndFlush(user);
            logger.info("密码更改成功");
            return true;
        }else {
            logger.error("原密码或者验证码错误，更改失败");
            return false;
        }
    }
    logger.error("查无此用户，更改失败");
    return false;
    }

    /**
     * 根据用户名，密保问题，密保答案找回密码ServiceImpl
     * @param userName
     * @param email
     * @param httpSession
     * @param verificationCode
     * @param encryptedProblem
     * @param encryptedAnswer
     * @return boolean
     */
    @Override
    public boolean findPasswordService(String userName,String email,HttpSession httpSession, String verificationCode, String encryptedProblem, String encryptedAnswer) {
        List<User> userList=userDaoRepository.findUsersByUserName(userName);
        Iterator<User> userIterator=userList.iterator();
        while (userIterator.hasNext()){
            User user=userIterator.next();
            if (user.getEncryptedProblem().equals(encryptedProblem)&&user.getEncryptedAnswer().equals(encryptedAnswer)&&httpSession.getAttribute("findPasswordverificationCode").equals(verificationCode)&&user.getEmail().equals(email)){
                logger.info("密码找回成功");
                httpSession.setAttribute("findPassword",user.getPassword());
                return true;
            }else {
                logger.error("密保问题，密保答案，邮箱或验证码错误");
                return false;
            }
        }
        return false;
    }
}
